package game;

import java.awt.geom.Point2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;

/**
 * 
 * @author Shun Fan
 * This class represents a level in bubblefish
 */
public class Level {
	PathPoint path[];
    int path_inc_x;
    int path_inc_y;
    int num_path_points;
    double path_speed;
    int path_start_t;
    int fishToSaveAtStartOfLevel;
    TxtParser txtParser = new TxtParser();

    public Level() {
        File levelFile = new File("resources/level1properties.txt");
        
        TxtParser textParser = new TxtParser(levelFile);
        fishToSaveAtStartOfLevel = textParser.getFishLeftToSave();
    	path_speed = textParser.getBubblePathSpeed();
        path_inc_x = textParser.getPathXOffset();
        path_inc_y = textParser.getPathYOffset();
    	path_start_t = textParser.getBubblePathStartPos();
    	num_path_points = textParser.getNumberOfBubblePathPoints();
    	path = new PathPoint[num_path_points];
    	ArrayList<Point2D.Double> pathPoints = textParser.getBubblePathPoints();
        for (int i = 0; i < pathPoints.size(); i++) {
         	setPathPoint(i,(int) pathPoints.get(i).x,(int) pathPoints.get(i).y);
        }
    }
    
    public Level(int sub_level) {
    	String levelFileName = "resources/properties/level"+sub_level+"properties.txt";
    	//System.out.println(levelFileName);
        File levelFile = new File(levelFileName);
        
        TxtParser textParser = new TxtParser(levelFile);
        fishToSaveAtStartOfLevel = textParser.getFishLeftToSave();
    	path_speed = textParser.getBubblePathSpeed();
        path_inc_x = textParser.getPathXOffset();
        path_inc_y = textParser.getPathYOffset();
    	path_start_t = textParser.getBubblePathStartPos();
    	num_path_points = textParser.getNumberOfBubblePathPoints();
    	path = new PathPoint[num_path_points];
    	ArrayList<Point2D.Double> pathPoints = textParser.getBubblePathPoints();
        for (int i = 0; i < pathPoints.size(); i++) {
         	setPathPoint(i,(int) pathPoints.get(i).x,(int) pathPoints.get(i).y);
        }
    }

	public double getBubblePathSpeed() {
    	return path_speed;
    }
    
    public int getFishLeftToSave() {
    	return fishToSaveAtStartOfLevel;
    }
    
    public int getBubblePathStartPos() {
    	return path_start_t;
    }
    
    public int getNumberOfBubblePathPoints() {
    	return num_path_points;
    }
    
    public PathPoint[] getBubblePath(){
    	return path;
    }

	void setPathPoint (int idx, double x, double y)
    {
        path[idx] = new PathPoint();
        path[idx].x = (x + path_inc_x) * 0.71;
        path[idx].y = (y + path_inc_y - 10) * 0.71;
        if (idx > 0)
        {
            double dx = path[idx - 1].x - path[idx].x;
            double dy = path[idx - 1].y - path[idx].y;
            path[idx - 1].dist_to_next = Math.sqrt(dx * dx + dy * dy);
        }
        num_path_points = idx + 1;
    }
	
}
